package com.example.studyroom.controller;
/*
    自习室管理员功能控制类
*/

import com.example.studyroom.Entity.StudyRoomAdmin;
import com.example.studyroom.dto.StudyRoomAdmin.RoomAdminDTO;
import com.example.studyroom.dto.StudyRoomAdmin.HandleReservationDTO;
import com.example.studyroom.dto.StudyRoomAdmin.StudyRoomAdminLoginDTO;
import com.example.studyroom.service.ReservationService;
import com.example.studyroom.service.StudyRoomAdminService;
import com.example.studyroom.service.StudyRoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/studyroomadmin")
public class StudyRoomAdminController {

    @Autowired
    private StudyRoomAdminService studyRoomAdminService; // 注入 StudyRoomAdminService 依赖

    @Autowired
    private StudyRoomService studyRoomService; // 注入 StudyRoomService 依赖

    @Autowired
    private ReservationService reservationService; // 注入 ReservationService 依赖

    /**
     * 管理员登录
     * @param studyRoomAdminLoginDTO 包含管理员登录信息的 DTO
     * @return 成功登录返回 "登录成功!"，否则返回 "登录失败"
     */
    @PostMapping("/login")
    public ResponseEntity<String> login(@RequestBody StudyRoomAdminLoginDTO studyRoomAdminLoginDTO) {
        StudyRoomAdmin existingStudyRoomAdmin = studyRoomAdminService.findByStudyRoomAdminLogin(
                studyRoomAdminLoginDTO.getRname(), studyRoomAdminLoginDTO.getRpass()
        );

        if (existingStudyRoomAdmin != null) {
            return ResponseEntity.ok("登录成功!");
        } else {
            return ResponseEntity.badRequest().body("登录失败");
        }
    }

    /**
     * 查看该管理员负责的自习室
     * @param rname 管理员用户名
     * @return 返回该管理员负责的自习室列表
     */
    @GetMapping("/myroom")
    public ResponseEntity<List<RoomAdminDTO>> getStudyRoomsByAdmin(@RequestParam String rname) {
        List<RoomAdminDTO> roomAdminDTOS = studyRoomService.getStudyRoomsByAdmin(rname);
        return ResponseEntity.ok(roomAdminDTOS);
    }


    /**
     * 查看该管理员负责的自习室的所有预约
     * @param adminName 管理员用户名
     * @return 返回该管理员负责的自习室的所有预约列表
     */
    @GetMapping("/getReservationByAdmin")
    public ResponseEntity<List<HandleReservationDTO>> getReservationByAdmin(@RequestParam String adminName) {
        List<HandleReservationDTO> handleReservationDTOS = reservationService.getReservationByAdmin(adminName);
        return ResponseEntity.ok(handleReservationDTOS);
    }

    /**
     * 审核预约
     * @param id 预约的 ID
     * @return 状态 200 的空响应
     */
    @PutMapping("/handlereservation/{id}")
    public ResponseEntity<Void> handleReservation(@PathVariable Integer id) {
        reservationService.handleReservation(id);
        return ResponseEntity.ok().build();
    }
}
